package com.qf.controller;

import com.qf.entity.Data;
import com.qf.entity.User;
import com.qf.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

@CrossOrigin("*")
@RestController
@RequestMapping("/Register")
public class RegisterController {
    private UserService userService;
    public RegisterController(UserService userService){

        this.userService = userService;

    }
    @PostMapping
    private Data AddUser(HttpSession session,String username,String password,String email,@RequestParam(value = "gender") String sex,String phone)throws ServletException, IOException {
        User user = new User();
        user.setUName(username);
        user.setUPassword(password);
        user.setUEmail(email);
        user.setUSex(sex);
        user.setUPhone(phone);
        user.setUStatus(1);
        user.setURegisterDate(new Date());
        user.setUBalance(BigDecimal.valueOf(100000));
        System.out.println(user);
        Data data = null;

        if(user != null){
            userService.insert(user);
            session.setAttribute("user",user);
            data = new Data(1,"注册成功！");
            return data;

        }else {

            data = new Data(-1,"注册失败！");
            return data;

        }




    }


}
